package com.hxd.mall.utils.open

import android.annotation.SuppressLint
import android.graphics.drawable.Drawable
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.graphics.drawable.toBitmap
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy.NONE
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.RequestOptions.bitmapTransform
import com.bumptech.glide.request.RequestOptions.circleCropTransform
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.hxd.mall.widget.CenterCropRoundCornerTransform
import com.shizhefei.view.largeimage.LargeImageView
import android.R


/**
 * Create by yinzhengwei on 2019/3/12
 * @Function
 */
@SuppressLint("CheckResult")
fun ImageView.load(
    path: Any?,
    defaultDrawableId: Int = 0,
    errorDrawableId: Int = 0,
    corner: Int = 15,
    isCache: Boolean = true,
    scaletype: ImageView.ScaleType = ImageView.ScaleType.FIT_XY
) {
    val target = Glide.with(context).load(path ?: "")

    //判断是否有默认图、缓存、加载失败图
    val placeholder =
        if (corner == 0) RequestOptions() else bitmapTransform(CenterCropRoundCornerTransform(corner))

    if (defaultDrawableId != 0) {
        placeholder.placeholder(defaultDrawableId)
    }
    if (!isCache) {
        placeholder.diskCacheStrategy(NONE)
    }
    if (errorDrawableId != 0)
        placeholder.error(errorDrawableId)

    target.apply(placeholder)

    scaleType = scaletype

    target.into(this)
}

/**
 * Create by yinzhengwei on 2019/3/12
 * @Function
 */
@SuppressLint("CheckResult")
fun ImageView.loadRectangularImg(
    path: Any?,
    defaultDrawableId: Int = 0,
    errorDrawableId: Int = 0,
    corner: Int = 15,
    isCache: Boolean = true,
    scaletype: ImageView.ScaleType = ImageView.ScaleType.FIT_XY,
    iv: ImageView,
    ivWidth: Int? = -1,
    ivHeight: Int? = -1
) {
    val target = Glide.with(context).load(path ?: "")

    //判断是否有默认图、缓存、加载失败图
    val placeholder = bitmapTransform(CenterCropRoundCornerTransform(corner))

    if (defaultDrawableId != 0) {
        placeholder.placeholder(defaultDrawableId)
    }
    if (!isCache) {
        placeholder.diskCacheStrategy(NONE)
    }
    if (errorDrawableId != 0)
        placeholder.error(errorDrawableId)

    target.apply(placeholder)

    scaleType = scaletype

    //iv.setImageDrawable(null)
    target.into(object : SimpleTarget<Drawable>() {
        override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
            val params = iv.layoutParams
            if (ivHeight != null && ivHeight != -1) {
                params?.height = ivHeight
            }
            if (ivWidth != null && ivWidth != -1) {
                params?.width = ivWidth
            }
            layoutParams = params
            setImageDrawable(resource)
        }

        override fun onStart() {
            super.onStart()

            iv.setImageResource(defaultDrawableId)
        }
    }
    )
}

/**
 * Create by yinzhengwei on 2019/3/12
 * @Function
 */
@SuppressLint("CheckResult")
fun ImageView.loadRectangularImg2(
    path: Any?,
    defaultDrawableId: Int = 0,
    errorDrawableId: Int = 0,
    corner: Int = 15,
    isCache: Boolean = true,
    scaletype: ImageView.ScaleType = ImageView.ScaleType.FIT_XY,
    iv: ImageView,
    ivWidth: Int? = -1,
    ivHeight: Int? = -1
) {
    val target = Glide.with(context).load(path ?: "")

    //判断是否有默认图、缓存、加载失败图
    val placeholder = bitmapTransform(CenterCropRoundCornerTransform(corner))

    if (defaultDrawableId != 0) {
        placeholder.placeholder(defaultDrawableId)
    }
    if (!isCache) {
        placeholder.diskCacheStrategy(NONE)
    }
    if (errorDrawableId != 0)
        placeholder.error(errorDrawableId)

    target.apply(placeholder)

    scaleType = scaletype

    //iv.setImageDrawable(null)
    target.into(object : SimpleTarget<Drawable>() {
        override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
            val params = iv.layoutParams
            if (ivHeight != null && ivHeight != -1) {
                params?.height = ivHeight
            }
            if (ivWidth != null && ivWidth != -1) {
                params?.width = ivWidth
            }
            layoutParams = params
            setImageDrawable(resource)
        }
    }
    )
}

@SuppressLint("CheckResult")
fun ImageView.loadCircle(
    path: String?,
    defaultDrawableId: Int = 0,
    errorDrawableId: Int = 0,
    isCache: Boolean = true,
    isFormHome: Boolean? = false
) {
    val target = Glide.with(context).load(path ?: "")

    //判断是否有默认图、缓存、加载失败图
    val placeholder = if (isFormHome == true) RequestOptions() else circleCropTransform()


    if (defaultDrawableId != 0) {
        placeholder.placeholder(defaultDrawableId)
    }
    if (!isCache) {
        placeholder.diskCacheStrategy(NONE)
    }
    if (errorDrawableId != 0)
        placeholder.error(errorDrawableId)

    target.apply(placeholder)

//    scaleType = ImageView.ScaleType.FIT_XY

    target.into(this)
}

fun ImageView.loadGif(path: Any, defaultDrawableId: Int = 0, errorDrawableId: Int = 0) {
    Glide.with(this)
        .asGif()
        .load(path)
        //.placeholder(defaultDrawableId)
        //.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
        //.error(errorDrawableId)
        .into(this)
}

@SuppressLint("CheckResult")
fun LargeImageView.load(
    path: String,
    defaultDrawableId: Int,
    errorDrawableId: Int = 0,
    isCache: Boolean = true
) {
    val target = Glide.with(context).load(path)

    //判断是否有默认图、缓存、加载失败图
    val placeholder = RequestOptions()
    if (defaultDrawableId != 0) {
        placeholder.placeholder(defaultDrawableId)
    }
    if (!isCache) {
        placeholder.diskCacheStrategy(NONE)
    }
    if (errorDrawableId != 0)
        placeholder.error(errorDrawableId)
    target.apply(placeholder)

    target.into(object : SimpleTarget<Drawable>() {
        override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {

            val bitmap = resource.toBitmap()
            onLoadImageSize(bitmap.width, bitmap.height)

            setImage(resource)
        }
    })
}

@SuppressLint("CheckResult")
fun TextView.load(
    path: String,
    defaultDrawableId: Int? = 0,
    errorDrawableId: Int? = 0,
    isCache: Boolean = true
) {
    val target = Glide.with(context).load(path)

    //判断是否有默认图、缓存、加载失败图
    val placeholder = RequestOptions()
    if (defaultDrawableId != 0) {
        placeholder.placeholder(defaultDrawableId ?: 0)
    }
    if (!isCache) {
        placeholder.diskCacheStrategy(NONE)
    }
    if (errorDrawableId != 0)
        placeholder.error(errorDrawableId ?: 0)
    target.apply(placeholder)

    target.into(object : SimpleTarget<Drawable>() {
        override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
            background = resource
        }
    })
}

@SuppressLint("CheckResult")
fun TextView.loadRectangularImg(
    path: String,
    defaultDrawableId: Int? = 0,
    errorDrawableId: Int? = 0,
    isCache: Boolean = true
) {
    val target = Glide.with(context).load(path)

    //判断是否有默认图、缓存、加载失败图
    val placeholder = RequestOptions()
    if (defaultDrawableId != 0) {
        placeholder.placeholder(defaultDrawableId ?: 0)
    }
    if (!isCache) {
        placeholder.diskCacheStrategy(NONE)
    }
    if (errorDrawableId != 0)
        placeholder.error(errorDrawableId ?: 0)
    target.apply(placeholder)

    target.into(object : SimpleTarget<Drawable>() {
        override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
            /*val bitmap = resource.toBitmap()
            val params = helper.itemView.layoutParams

            val height = (bitmap.height * imageView.context.displayMetrics.widthPixels) / bitmap.width
            params.height = height
            helper.itemView.layoutParams = params
            imageView.setImage(resource)*/
            background = resource
        }
    })
}


@SuppressLint("CheckResult")
fun ViewGroup.load(
    path: String,
    defaultDrawableId: Int? = 0,
    errorDrawableId: Int? = 0,
    isCache: Boolean = true
) {
    val target = Glide.with(context).load(path)

    //判断是否有默认图、缓存、加载失败图
    val placeholder = RequestOptions()
    if (defaultDrawableId != 0) {
        placeholder.placeholder(defaultDrawableId ?: 0)
    }
    if (!isCache) {
        placeholder.diskCacheStrategy(NONE)
    }
    if (errorDrawableId != 0)
        placeholder.error(errorDrawableId ?: 0)
    target.apply(placeholder)

    target.into(object : SimpleTarget<Drawable>() {
        override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
            background = resource
        }
    })
}